package com.blt.car.home.chosecity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.base.BaseTitleBarActivity;
import com.blt.car.base.ViewHolder;

import java.util.List;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.other.OutApi;


/**
 * Created by xgp on 2017/6/16.
 */

public class ChooseCityActivity extends BaseTitleBarActivity implements View.OnClickListener {
    public static final int REQUEST_CODE_PICK_CITY = 2333;
    public static final String KEY_PICKED_CITY = "picked_city";
    public static final String CUR_CITY = "cur_city";

    private ListView mListView;
    private ListView mResultListView;
    private SideLetterBar mLetterBar;
    private EditText searchBox;
    private ImageView clearBtn;
    private ViewGroup emptyView;

    private CityListAdapter mCityAdapter;
    private ResultListAdapter mResultAdapter;
    private List<City> mAllCities;
    private DBManager dbManager;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_city_list;
    }

    @Override
    protected void initView(ViewHolder holder) {
        mTitleBar.setTitle("城市选择");
        String city = null;
        if (getIntent().getExtras() != null) {
            city = getIntent().getExtras().getString(CUR_CITY);
        }
        TextView tvCity = holder.get(R.id.chose_city_cur_city);
        tvCity.setText("当前城市:" + city);
        mListView = holder.get(R.id.listview_all_city);
        mListView.setAdapter(mCityAdapter);
        TextView overlay = holder.get(R.id.tv_letter_overlay);
        mLetterBar = holder.get(R.id.side_letter_bar);
        mLetterBar.setOverlay(overlay);
        mLetterBar.setOnLetterChangedListener(new SideLetterBar.OnLetterChangedListener() {
            @Override
            public void onLetterChanged(String letter) {
                int position = mCityAdapter.getLetterPosition(letter);
                mListView.setSelection(position);
            }
        });
        searchBox = holder.get(R.id.et_search);
        searchBox.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyword = s.toString();
                if (TextUtils.isEmpty(keyword)) {
                    clearBtn.setVisibility(View.GONE);
                    emptyView.setVisibility(View.GONE);
                    mResultListView.setVisibility(View.GONE);
                } else {
                    clearBtn.setVisibility(View.VISIBLE);
                    mResultListView.setVisibility(View.VISIBLE);
                    List<City> result = dbManager.searchCity(keyword);
                    if (result == null || result.size() == 0) {
                        emptyView.setVisibility(View.VISIBLE);
                    } else {
                        emptyView.setVisibility(View.GONE);
                        mResultAdapter.changeData(result);
                    }
                }
            }
        });
        emptyView = holder.get(R.id.empty_view);
        mResultListView = holder.get(R.id.listview_search_result);
        mResultListView.setAdapter(mResultAdapter);
        mResultListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String cityName = mResultAdapter.getItem(position).getName();
                CoreManager.notifyClients(OutApi.class, OutApi.onLocation, cityName);
                finish();
//                toastShort(mResultAdapter.getItem(position).getName());
            }
        });

        clearBtn = holder.get(R.id.iv_search_clear);
        clearBtn.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        dbManager = new DBManager(this);
        dbManager.copyDBFile();
        mAllCities = dbManager.getAllCities();
        mCityAdapter = new CityListAdapter(this, mAllCities);
        mCityAdapter.setOnCityClickListener(new CityListAdapter.OnCityClickListener() {
            @Override
            public void onCityClick(String name) {
//                toastShort(name);
                CoreManager.notifyClients(OutApi.class, OutApi.onLocation, name);
                finish();
            }


        });
        mResultAdapter = new ResultListAdapter(this, null);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_search_clear:
                searchBox.setText("");
                clearBtn.setVisibility(View.GONE);
                emptyView.setVisibility(View.GONE);
                mResultListView.setVisibility(View.GONE);
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
